package de.sisoft.gcjack.view;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ListView;

import com.bugsense.trace.BugSenseHandler;

import de.sisoft.gcjack.R;
import de.sisoft.gcjack.adapter.SideNavListAdapter;
import de.sisoft.gcjack.view.sidenav.SideNavEntry;
import de.sisoft.gcjack.view.sidenav.SideNavItem;
import de.sisoft.gcjack.view.sidenav.SideNavSection;

public class FragmentContainer extends Activity {
	private DrawerLayout			menuLayout;
	private ListView				menuList;
	private ActionBarDrawerToggle	menuToggle;

	// nav drawer title
	private CharSequence			menuTitle;

	// used to store app title
	private CharSequence			appTitle;

	// slide menu entires
	private String[]				sideNavItemTitles;
	private TypedArray				sideNavEntryIcons;

	private ArrayList<SideNavItem>	sideNavItems;
	private SideNavListAdapter		sideNavListAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		BugSenseHandler.initAndStartSession(this, "089658b0");
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		setContentView(R.layout.fragment_container);

		appTitle = menuTitle = getTitle();

		// load slide menu items
		sideNavItemTitles = getResources().getStringArray(R.array.side_nav_items);

		// nav drawer icons from resources
		sideNavEntryIcons = getResources().obtainTypedArray(R.array.side_nav_icons);

		menuLayout = (DrawerLayout) findViewById(R.id.fragment_container_drawer_layout);
		menuList = (ListView) findViewById(R.id.list_side_nav_menu);

		sideNavItems = new ArrayList<SideNavItem>();

		// adding nav drawer items to array
		// Desiretours Account
		sideNavItems.add(new SideNavEntry(sideNavItemTitles[0], sideNavEntryIcons.getResourceId(0, -1)));
		// Section GeoCache
		sideNavItems.add(new SideNavSection(sideNavItemTitles[1]));
		// Find GeoCache
		sideNavItems.add(new SideNavEntry(sideNavItemTitles[2], sideNavEntryIcons.getResourceId(1, -1)));
		// Upload Votes
		sideNavItems.add(new SideNavEntry(sideNavItemTitles[3], sideNavEntryIcons.getResourceId(2, -1)));

		// Section App
		sideNavItems.add(new SideNavSection(sideNavItemTitles[4]));
		// Settings
		sideNavItems.add(new SideNavEntry(sideNavItemTitles[5], sideNavEntryIcons.getResourceId(3, -1)));
		// About
		sideNavItems.add(new SideNavEntry(sideNavItemTitles[6], sideNavEntryIcons.getResourceId(4, -1)));
		// Rate App
		sideNavItems.add(new SideNavEntry(sideNavItemTitles[7], sideNavEntryIcons.getResourceId(5, -1)));
		// Visit Homepage

		// Section Web
		sideNavItems.add(new SideNavSection(sideNavItemTitles[8]));
		// Share
		sideNavItems.add(new SideNavEntry(sideNavItemTitles[9], sideNavEntryIcons.getResourceId(6, -1)));
		sideNavItems.add(new SideNavEntry(sideNavItemTitles[10], sideNavEntryIcons.getResourceId(7, -1)));
		// //
		// sideNavItems.add(new SideNavEntry(sideNavItemTitles[7],
		// sideNavEntryIcons.getResourceId(7, -1)));

		// Recycle the typed array
		// sideNavEntryIcons.recycle();

		menuList.setOnItemClickListener(new SlideMenuClickListener());

		// setting the nav drawer list adapter
		sideNavListAdapter = new SideNavListAdapter(getApplicationContext(), sideNavItems);
		menuList.setAdapter(sideNavListAdapter);

		// enabling action bar app icon and behaving it as toggle button
		getActionBar().setDisplayHomeAsUpEnabled(true);
		getActionBar().setHomeButtonEnabled(true);

		menuToggle = new ActionBarDrawerToggle(this, menuLayout, R.drawable.ic_drawer, // nav
																						// menu
																						// toggle
																						// icon
				R.string.app_name, // nav drawer open - description for
				// accessibility
				R.string.app_name // nav drawer close - description for
		// accessibility
		) {
			public void onDrawerClosed(View view) {
				getActionBar().setTitle(appTitle);
				// calling onPrepareOptionsMenu() to show action bar icons
				invalidateOptionsMenu();
			}

			public void onDrawerOpened(View drawerView) {
				getActionBar().setTitle(menuTitle);
				// calling onPrepareOptionsMenu() to hide action bar icons
				invalidateOptionsMenu();
			}
		};
		menuLayout.setDrawerListener(menuToggle);

		if (savedInstanceState == null) {
			// on first time display view for first nav item
			displayView(0);
		}
	}

	/**
	 * Slide menu item click listener
	 * */
	private class SlideMenuClickListener implements ListView.OnItemClickListener {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			// display view for selected nav drawer item
			displayView(position);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// toggle nav drawer on selecting action bar app icon/title
		if (menuToggle.onOptionsItemSelected(item)) {
			return true;
		}
		// Handle action bar actions click
		switch (item.getItemId()) {
			case R.id.action_settings:
				return true;
			default:
				return super.onOptionsItemSelected(item);
		}
	}

	// /***
	// * Called when invalidateOptionsMenu() is triggered
	// */
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		// if nav drawer is opened, hide the action items
		boolean drawerOpen = menuLayout.isDrawerOpen(menuList);
		menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
		return super.onPrepareOptionsMenu(menu);
	}

	// /**
	// * Diplaying fragment view for selected nav drawer list item
	// * */
	private void displayView(int position) {
		// update the main content by replacing fragments
		Fragment fragment = null;
		switch (position) {
			case 0:
				fragment = new RegisterUserFragment();
				break;
			case 2:
				fragment = new FindGeoCacheFragment();
				break;
			case 3:
				fragment = new RegisterUserFragment();
				break;
			case 5:
				fragment = new RegisterUserFragment();
				break;
			case 6:
				fragment = new RegisterUserFragment();
				break;
			case 7:
				fragment = new RegisterUserFragment();
				break;
			case 9:
				fragment = new RegisterUserFragment();
				break;
			case 10:
				fragment = new RegisterUserFragment();
				break;

			default:
				break;
		}

		if (fragment != null) {
			FragmentManager fragmentManager = getFragmentManager();
			fragmentManager.beginTransaction().replace(R.id.fragment_container_frame, fragment).commit();

			// update selected item and title, then close the drawer
			menuList.setItemChecked(position, true);
			menuList.setSelection(position);
			setTitle(sideNavItemTitles[position]);
			this.menuLayout.closeDrawer(menuList);
		} else {
			// error in creating fragment
			Log.e("MainActivity", "Error in creating fragment");
		}
	}

	@Override
	public void setTitle(CharSequence title) {
		appTitle = title;
		getActionBar().setTitle(appTitle);
	}

	/**
	 * When using the ActionBarDrawerToggle, you must call it during
	 * onPostCreate() and onConfigurationChanged()...
	 */
	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);
		// Sync the toggle state after onRestoreInstanceState has occurred.
		menuToggle.syncState();
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		// Pass any configuration change to the drawer toggls
		menuToggle.onConfigurationChanged(newConfig);
	}
}
